import React from "react";
import * as Icon from "@ant-design/icons";

// 找到当前路由的父级路由
const generatePaths = (inputPath) => {
  const parts = inputPath.split("/").filter((item) => item !== "");
  const paths = [];
  let currentPath = "";

  for (const part of parts) {
    currentPath += `/${part}`;
    paths.push(currentPath);
  }
  return paths.slice(0, -1);
};

// 字符串icon转换为元素
const iconToElement = (name) => React.createElement(Icon[name]);

// 生成路由
const generateRoutes = (menuItems) => {
  return menuItems.map((item) => {
    if (item.children) {
      return {
        key: item.path,
        icon: iconToElement(item.icon),
        label: item.label,
        children: generateRoutes(item.children),
      };
    } else {
      return {
        key: item.path,
        icon: iconToElement(item.icon),
        label: item.label,
      };
    }
  });
};

export {
  generatePaths,
  generateRoutes
}